package com.rou.im.framework.core.context;

/**
 * <p>
 *     上下文持有人
 * </p>
 *
 * @author GaoShuangWen
 * @since 2020-04-19 11:31
 */
public class RequestContextHolder {

    private static final ThreadLocal<RequestContext> contextHolder = new ThreadLocal<>();

    public static void clearContext() {
        contextHolder.remove();
    }

    public static RequestContext getContext() {
        RequestContext context = contextHolder.get();

        if (context == null) {
            context = createEmptyContext();
            contextHolder.set(context);
        }

        return context;
    }

    public static void setContext(RequestContext context) {
        contextHolder.set(context);
    }

    public static RequestContext createEmptyContext() {
        return new RequestContextImpl();
    }
}
